//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef _USB11_H_
#define _USB11_H_

typedef struct {
  unsigned char bmRequest, bRequest;
  unsigned short wValue, wIndex, wLength;
} SetupPKG, *PSetupPKG;

// Request Codes - decimal
#define STD_GET_STATUS  	 0
#define STD_CLEAR_FEATURE    1
#define STD_SET_FEATURE      3
#define STD_SET_ADDRESS      5
#define STD_GET_DESCRIPTOR   6
#define STD_SET_DESCRIPTOR   7
#define STD_GET_CONFIG       8
#define STD_SET_CONFIG       9
#define STD_GET_INTERFACE   10
#define STD_SET_INTERFACE   11
#define STD_SYNCH_FRAME		12

//-------------------------------------------------------------------------
// Standard Chapter 9 definition
//-------------------------------------------------------------------------

// Request Codes
#define GET_STATUS      0x00
#define CLEAR_FEATURE   0x01
#define SET_FEATURE     0x03
#define SET_ADDRESS     0x05
#define GET_DESCRIPTOR  0x06
#define SET_DESCRIPTOR  0x07
#define GET_CONFIG      0x08
#define SET_CONFIG      0x09
#define GET_INTERFACE   0x0A
#define SET_INTERFACE   0x0B
#define SYNCH_FRAME     0x0C
#define VENDOR_COMMAND  0x60

// Standard Feature Selectors
#define DEVICE_REMOTE_WAKEUP        0x1
#define ENDPOINT_HALT               0x0

/*
 * Device and/or Interface Class codes
 * as found in bDeviceClass or bInterfaceClass
 * and defined by www.usb.org documents
 */
#define USB_CLASS_PER_INTERFACE	0	/* for DeviceClass */
#define USB_CLASS_AUDIO			1
#define USB_CLASS_COMM			2
#define USB_CLASS_HID			3
#define USB_CLASS_PHYSICAL		5
#define USB_CLASS_STILL_IMAGE	6
#define USB_CLASS_PRINTER		7
#define USB_CLASS_MASS_STORAGE  8
#define USB_CLASS_HUB			9
#define USB_CLASS_CDC_DATA		0x0a
#define USB_CLASS_CSCID			0x0b	/* chip+ smart card */
#define USB_CLASS_CONTENT_SEC   0x0d	/* content security */
#define USB_CLASS_VIDEO			0x0e
#define USB_CLASS_APP_SPEC		0xfe
#define USB_CLASS_VENDOR_SPEC	0xff

/*
 * USB directions
 *
 * This bit flag is used in endpoint descriptors' bEndpointAddress field.
 * It's also one of three fields in control requests bRequestType.
 */
#define USB_DIR_OUT			0x00		/* to device */
#define USB_DIR_IN			0x80		/* to host */

/* USB types, the second of three bRequestType fields */
#define USB_TYPE_MASK			(0x03 << 5)
#define USB_TYPE_STANDARD		(0x00 << 5)
#define USB_TYPE_CLASS			(0x01 << 5)
#define USB_TYPE_VENDOR			(0x02 << 5)
#define USB_TYPE_RESERVED		(0x03 << 5)

/* Descriptor Types */
#define USB_DT_DEVICE			0x01
#define USB_DT_CONFIG			0x02
#define USB_DT_STRING			0x03
#define USB_DT_INTERFACE		0x04
#define USB_DT_ENDPOINT			0x05

/* descriptor size */
#define USB_DT_DEVICE_SIZE		18
#define USB_DT_CONFIG_SIZE		9
#define USB_DT_INTERFACE_SIZE	9
#define USB_DT_ENDPOINT_SIZE	7

/* Endpoints */
#define USB_ENDPOINT_XFER_CONTROL	0
#define USB_ENDPOINT_XFER_ISOC		1
#define USB_ENDPOINT_XFER_BULK		2
#define USB_ENDPOINT_XFER_INT		3

/* from config descriptor bmAttributes */
#define USB_CONFIG_ATT_ONE		(1 << 7)	/* must be set */
#define USB_CONFIG_ATT_SELFPOWER	(1 << 6)	/* self powered */
#define USB_CONFIG_ATT_WAKEUP		(1 << 5)	/* can wakeup */


#define STR_INDEX_MANUFACTURER      0x00
#define STR_INDEX_PRODUCT           0x01
#define STR_INDEX_SERIALNUMBER      0x02

#define WAIT_FOR_SETUP		1
#define DATA_STATE_XMIT		2
#define DATA_STATE_RCVR		3
#define WAIT_FOR_OUT_STATUS	4
#define WAIT_FOR_IN_STATUS	5

//USB Interrupt Type
typedef UInt32 INTRTYPE;
#define INTR_NO_INTR     0x00000000
#define INTR_HOST_RESET  0x00000001
#define INTR_CFG_CHANGE  0x00000002
#define INTR_CTRL_XFER   0x00000004
#define INTR_XMIT_XFER   0x00000008
#define INTR_RECV_XFER   0x00000010


//#pragma pack(push, 1)
#if defined(_INTEL) || defined(_EVC)
#undef DECL_PACKED
#define DECL_PACKED
#pragma pack(push, 1)
#endif
typedef struct DECL_PACKED __device_descriptor {
    UInt8   bLength;
    UInt8   bDescriptorType;
    UInt16  bcdUSB;
    UInt8   bDeviceClass;
    UInt8   bDeviceSubClass;
    UInt8   bDeviceProtocol;
    UInt8   bMaxPackectSize0;
    UInt16  idVendor;
    UInt16  idProduct;
    UInt16  bcdDevice;
    UInt8   iManufacturer;
    UInt8   iProduct;
    UInt8   iSerialNumber;
    UInt8   bNumConfigurations;
}device_descriptor, *Pdevice_descriptor;

typedef struct DECL_PACKED __configuration_descriptor {
    UInt8   bLength;
    UInt8   bDescriptorType;
    UInt16  wTotalLength;
    UInt8   bNumInterfaces;
    UInt8   bConfigurationValue;
    UInt8   iConfiguration;
    UInt8   bmAttributes;
    UInt8   bMaxPower;
}configuration_descriptor, *Pconfiguration_descriptor;

typedef struct DECL_PACKED __interface_descriptor {
    UInt8   bLength;
    UInt8   bDescriptorType;
    UInt8   bInterfaceNumber;
    UInt8   bAlternateSetting;
    UInt8   bNumEndpoints;
    UInt8   bInterfaceClass;
    UInt8   bInterfaceSubClass;
    UInt8   bInterfaceProtocol;
    UInt8   iInterface;
}interface_descriptor, *Pinterface_descriptor;


typedef struct DECL_PACKED __endpoint_descriptor
{
	UInt8	bLength;            //0
	UInt8	bDescriptorType;    //1
	UInt8	bEndpointAddress;   //2
	UInt8	bmAtrributes;       //3
	UInt16	wMaxPacketSize;     //4
	UInt8	bInterval;          //6

}endpoint_descriptor,*Pendpoint_descriptor;

typedef struct DECL_PACKED __string_descriptor {
    UInt8 *pbString;
    UInt8 bLength;
}string_descriptor, *Pstring_descriptor;

typedef struct DECL_PACKED __descriptor {
    string_descriptor        String;
    device_descriptor        Device;
    configuration_descriptor Config;
    UInt8                   *ConfigData;
//    interface_descriptor     Interface;
//    endpoint_descriptor      Endpoint;
} descriptor, *Pdescriptor;
#if defined(_INTEL) || defined(_EVC)
#pragma pack(pop)
#endif

#endif
